<?php

namespace App\Models;

use App\Models\ComModel;

class UploadsModel extends ComModel
{
    public $tablename;

    public function __construct(...$param)
    {
        parent::__construct();
        $this->tablename = 'uploads';
    }
    //新增和编辑全部数据时验证
    private function _validate($data, $id = 0)
    {
        if (!$data) {
            return ams_rt(1, '分类名称必须填写');
        }

        if (!$data['name']) {
            return ams_rt(1, '名称必须填写');
        } elseif (!$data['uri']) {
            return ams_rt(1, 'URI必须填写');
        }

        $data = esc($data);

        $flag = $this->field_exitsts($this->tablename, 'name', $data['name'], $id);
        if ($flag) {
            return ams_rt(1, '名称已存在');
        }

        $flag = $this->field_exitsts($this->tablename, 'uri', $data['uri'], $id);
        if ($flag) {
            return ams_rt(1, 'URI已存在');
        }

        return ams_rt(0, 'ok', $data);
    }

    public function add($data)
    {
        $rt = $this->_validate($data);
        if ($rt['code']) {
            return $rt;
        }
        $data = $rt['data'];
        $this->db->table($this->tablename)->insert($data);
        $id = $this->db->insertID();
        if ($id) {
            return ams_rt(0, '操作成功', $id);
        }
        return ams_rt(1, '操作失败', $data);
    }

    public function edit($id, $data)
    {

        if (!$id) {
            return ams_rt(1, '参数错误');
        }
        $rt = $this->_validate($data, $id);
        if ($rt['code']) {
            return $rt;
        }
        $data = $rt['data'];

        $flag = $this->db->table($this->tablename)->where('id', $id)->update($data);
        if ($flag) {
            return ams_rt(0, '操作成功', $id);
        }
        return ams_rt(1, '操作失败', $data);
    }

    public function get($id)
    {
        if (!$id) {
            return ams_rt(1, '参数错误');
        }
        $row = $this->db->table($this->tablename)->where('id', $id)->get()->getRowArray();
        if (!$row) {
            return [];
        }
        return $row;
    }

    public function del($id)
    {
        $id = intval($id);
        if (!$id) {
            return ams_rt(1, '参数错误');
        }
        //数据归属
        $row = $this->db->table($this->tablename)->where('id', $id)->get()->getRowArray();
        if (!$row) {
            return ams_rt(1, '数据不存在');
        }
        $this->db->table($this->tablename)->where('id', $id)->delete();
        return ams_rt(0, '操作成功');
    }

    // 分页
    public function limit_page($page = 0, $size = 10, $total = 0, $param = [])
    {

        $page = max(1, (int) $page);
        $total = (int) $total;

        unset($param['page']);
        if ($param) {
            $param = esc($param);
        }

        if ($size > 0 && !$total) {
            $select = $this->db->table($this->tablename)->select('count(*) as total');
            $param = $this->_limit_where($select, $param);
            $query = $select->get();
            if (!$query) {
                log_message('error', '数据查询失败：' . $this->tablename);
                return [[], $total, $param];
            }
            $data = $query->getRowArray();
            $total = (int) $data['total'];
            $param['total'] = $total;
            unset($select);
            if (!$total) {
                return [[], $total, $param];
            }
        }

        $select = $this->db->table($this->tablename);
        $order = 'id desc';
        $param = $this->_limit_where($select, $param);
        $size > 0 && $select->limit($size, $size * ($page - 1));
        $query = $select->orderBy($order)->get();
        if (!$query) {
            log_message('error', '数据查询失败：' . $this->tablename);
            return [[], $total, $param];
        }
        $data = $query->getResultArray();
        $param['order'] = $order;
        $param['total'] = $total;

        return [$data, $total, $param];

    }

    /**
     * 条件查询
     *
     * @param    object    $select    查询对象
     * @param    intval    $where    是否搜索
     * @return    intval
     */
    protected function _limit_where(&$select, $param)
    {
        // 条件搜索
        if ($param) {
            if (isset($param['name'])) {
                $select->like('name', $param['name']);
            }

            if (isset($param['uri'])) {
                $select->like('uri', $param['uri']);
            }

            if (isset($param['status']) && $param['status'] != 0) {
                $select->like('status', $param['status']);
            }
        }
        return $param;
    }

    /****上传地址****/
    // 分页
    public function limit_page_uri($page = 0, $size = 10, $total = 0, $param = [])
    {

        $page = max(1, (int) $page);
        $total = (int) $total;

        unset($param['page']);
        if ($param) {
            $param = esc($param);
        }

        if ($size > 0 && !$total) {
            $select = $this->db->table($this->tablename)->select('count(*) as total');
            $param = $this->_limit_where_uri($select, $param);
            $query = $select->get();
            if (!$query) {
                log_message('error', '数据查询失败：' . $this->tablename);
                return [[], $total, $param];
            }
            $data = $query->getRowArray();
            $total = (int) $data['total'];
            $param['total'] = $total;
            unset($select);
            if (!$total) {
                return [[], $total, $param];
            }
        }

        $select = $this->db->table($this->tablename);
        $order = 'id desc';
        $param = $this->_limit_where_uri($select, $param);
        $size > 0 && $select->limit($size, $size * ($page - 1));
        $query = $select->orderBy($order)->get();
        if (!$query) {
            log_message('error', '数据查询失败：' . $this->tablename);
            return [[], $total, $param];
        }
        $data = $query->getResultArray();
        $param['order'] = $order;
        $param['total'] = $total;

        return [$data, $total, $param];

    }

    /**
     * 条件查询
     *
     * @param    object    $select    查询对象
     * @param    intval    $where    是否搜索
     * @return    intval
     */
    protected function _limit_where_uri(&$select, $param)
    {
        // 条件搜索
        if ($param) {
            if (isset($param['name'])) {
                $select->like('name', urldecode($param['name']));
            }
        }
        return $param;
    }

    //添加存储策略
    public function addUri($data)
    {
        if (!$data) {
            return ams_rt(1, '参数错误');
        } elseif (!$data['name']) {
            return ams_rt(1, '请填写名称');
        }

        $data = esc($data);

        $count = $this->db->table('uploads_oss')->where('uri', $data['uri'])->countAllResults();
        if ($count) {
            return ams_rt(1, '类型已存在');
        }

        $data['value'] = json_encode($data['value']);

        $this->db->table('uploads_oss')->insert($data);
        $id = $this->db->insertID();
        if (!$id) {
            return ams_rt(1, '操作失败');
        }
        return ams_rt(0, '操作成功', $data);
    }

    //修改存储策略
    public function editUri($id, $data)
    {
        if (!$data || !$id) {
            return ams_rt(1, '参数错误');
        } elseif (!$data['name']) {
            return ams_rt(1, '请填写名称');
        }
        $data = esc($data);
        $id = intval($id);
        $count = $this->db->table('uploads_oss')->where('id <> ' . $id)->where('uri', $data['uri'])->countAllResults();
        if ($count) {
            return ams_rt(1, 'URI已存在');
        }
        $data['value'] = json_encode($data['value']);
        $this->db->table('uploads_oss')->where('id', $id)->update($data);
        return ams_rt(0, '操作成功', $data);
    }

    //删除存储策略
    public function delUri($id)
    {
        if (!$id) {
            return ams_rt(1, '参数错误');
        }
        $id = intval($id);
        $row = $this->db->table('uploads_oss')->where('id', $id)->get()->getRowArray();
        if (!$row) {
            return ams_rt(1, '数据不存在');
        }
        $this->db->table('uploads_oss')->where('id', $id)->delete();
        return ams_rt(0, '操作成功', $row);
    }

    /******存储策略*****/
    // 分页
    public function limit_page_oss($page = 0, $size = 10, $total = 0, $param = [])
    {

        $page = max(1, (int) $page);
        $total = (int) $total;

        unset($param['page']);
        if ($param) {
            $param = esc($param);
        }

        if ($size > 0 && !$total) {
            $select = $this->db->table($this->tablename . '_oss')->select('count(*) as total');
            $param = $this->_limit_where_oss($select, $param);
            $query = $select->get();
            if (!$query) {
                log_message('error', '数据查询失败：' . $this->tablename . '_oss');
                return [[], $total, $param];
            }
            $data = $query->getRowArray();
            $total = (int) $data['total'];
            $param['total'] = $total;
            unset($select);
            if (!$total) {
                return [[], $total, $param];
            }
        }

        $select = $this->db->table($this->tablename . '_oss');
        $param = $this->_limit_where_oss($select, $param);
        $size > 0 && $select->limit($size, $size * ($page - 1));
        $query = $select->orderBy('id', 'desc')->get();
        if (!$query) {
            log_message('error', '数据查询失败：' . $this->tablename . '_oss');
            return [[], $total, $param];
        }
        $data = $query->getResultArray();
        $param['total'] = $total;

        return [$data, $total, $param];

    }

    /**
     * 条件查询
     *
     * @param    object    $select    查询对象
     * @param    intval    $where    是否搜索
     * @return    intval
     */
    protected function _limit_where_oss(&$select, $param)
    {

        // 条件搜索
        if ($param) {

            if (isset($param['id']) && $param['id']) {
                $select->where('id', (int) $$param['id']);
            }

            if (isset($param['name']) && $param['name']) {
                $select->whereIn('name', $param['name']);
            }

            if (isset($param['code']) && $param['code']) {
                $select->like('code', $param['code']);
            }

        }
        return $param;
    }

    //新增和编辑全部数据时验证
    private function _validate_oss($data, $id = 0)
    {
        if (!$data) {
            return ams_rt(1, '参数错误');
        }

        if (!$data['name']) {
            return ams_rt(1, '名称必须填写');
        } elseif (!$data['code']) {
            return ams_rt(1, '平台标识必须填写');
        } elseif (!$data['key']) {
            return ams_rt(1, 'Key必须填写');
        } elseif (!$data['secret']) {
            return ams_rt(1, 'Secret必须填写');
        } elseif (!$data['bucket']) {
            return ams_rt(1, 'bucket必须填写');
        }

        $data = esc($data);

        $flag = $this->field_exitsts($this->tablename, 'name', $data['name'], $id);
        if ($flag) {
            return ams_rt(1, '名称已存在');
        }

        return ams_rt(0, 'ok', $data);
    }

    //添加存储策略
    public function addOss($data)
    {
        $rt = $this->_validate_oss($data);
        if ($rt['code']) {
            return $rt;
        }
        $data = $rt['data'];

        $this->db->table($this->tablename . '_oss')->insert($data);
        $id = $this->db->insertID();
        if (!$id) {
            return ams_rt(1, '操作失败');
        }
        return ams_rt(0, '操作成功', $data);
    }

    //修改存储策略
    public function editOss($id, $data)
    {
        if (!$data || !$id) {
            return ams_rt(1, '参数错误');
        }
        $rt = $this->_validate_oss($data);
        if ($rt['code']) {
            return $rt;
        }
        $data = $rt['data'];

        $id = intval($id);
        $count = $this->db->table($this->tablename . '_oss')->where('id <> ' . $id)->where('name', $data['name'])->countAllResults();
        if ($count) {
            return ams_rt(1, '名称已存在');
        }
        $this->db->table($this->tablename . '_oss')->where('id', $id)->update($data);
        return ams_rt(0, '操作成功', $data);
    }

    public function getOss($id)
    {
        if (!$id) {
            return ams_rt(1, '参数错误');
        }
        $id = intval($id);
        $row = $this->db->table($this->tablename . '_oss')->where('id', $id)->get()->getRowArray();
        if (!$row) {
            return [];
        }
        return $row;
    }

    //删除存储策略
    public function delOss($id)
    {
        if (!$id) {
            return ams_rt(1, '参数错误');
        }
        $id = intval($id);
        $row = $this->db->table($this->tablename . '_oss')->where('id', $id)->get()->getRowArray();
        if (!$row) {
            return ams_rt(1, '数据不存在');
        }
        $this->db->table($this->tablename . '_oss')->where('id', $id)->delete();
        return ams_rt(0, '操作成功', $row);
    }

}
